<html>
<head><meta charset="utf-8"><title>heap-allocation in const fn · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html">heap-allocation in const fn</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="159499385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159499385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159499385">(Feb 27 2019 at 10:58)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> the <code>constinit</code> proposal states that iff the initializer would need to execute at run-time, the program is ill-formed</p>



<a name="159499422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159499422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159499422">(Feb 27 2019 at 10:59)</a>:</h4>
<p>so maybe if you try to do that in C++ you just get a compilation error in the particular cases in which initializing a static variable from a constant-expression would need to heap-allocate.</p>



<a name="159499501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159499501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159499501">(Feb 27 2019 at 11:00)</a>:</h4>
<p>feels a bit brittle, but the proposals do not talk about each other yet - I've asked in the future_proposal channel of the C++ slack about it and pinged both authors</p>



<a name="159499871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159499871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159499871">(Feb 27 2019 at 11:07)</a>:</h4>
<p>kk</p>



<a name="159499922"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159499922" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159499922">(Feb 27 2019 at 11:08)</a>:</h4>
<p><code>constinit</code> seems closest to Rust's <code>const</code></p>



<a name="159499940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159499940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159499940">(Feb 27 2019 at 11:08)</a>:</h4>
<p>I wonder if maybe it should have been more like <code>constexpr</code>, but I am not sure if that would actually help much here</p>



<a name="159499950"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159499950" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159499950">(Feb 27 2019 at 11:09)</a>:</h4>
<p>(Cc <span class="user-mention" data-user-id="124288">@oli</span> <span class="user-mention" data-user-id="125253">@Nicole Mazzuca</span> )</p>



<a name="159500628"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159500628" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159500628">(Feb 27 2019 at 11:20)</a>:</h4>
<p>honestly the more i look into it, the more I think those who want constinit should just use <code>constexpr</code></p>



<a name="159500679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159500679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159500679">(Feb 27 2019 at 11:21)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">constexpr</span> <span class="k">auto</span> <span class="n">foo</span> <span class="o">=</span> <span class="n">maybe_allocates</span><span class="p">();</span>
<span class="n">constinit</span> <span class="k">static</span> <span class="k">auto</span> <span class="n">bar</span> <span class="o">=</span> <span class="n">foo</span><span class="p">;</span> <span class="c1">// OK unless foo allocated at compile-time</span>
</pre></div>


<p>seems brittle.</p>



<a name="159506142"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159506142" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159506142">(Feb 27 2019 at 12:47)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> reading some of the minutes from the last C++ meeting, it seems that they just banned non-transient allocations</p>



<a name="159516399"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159516399" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159516399">(Feb 27 2019 at 14:06)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> from <code>constinit</code>, or entirely?</p>



<a name="159516414"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159516414" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159516414">(Feb 27 2019 at 14:06)</a>:</h4>
<p>entirely</p>



<a name="159516582"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159516582" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159516582">(Feb 27 2019 at 14:07)</a>:</h4>
<p>allocations cannot escape <code>constexpr</code> functions, e.g., you can return an empty vector or string from a <code>constexpr</code> function, but not one that refers to some memory during constant-evaluation (at run-time it would work)</p>



<a name="159516791"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159516791" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159516791">(Feb 27 2019 at 14:09)</a>:</h4>
<p>oh, so that would mean the paper <span class="user-mention" data-user-id="125253">@Nicole Mazzuca</span> referenced is obsolete?</p>



<a name="159516822"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159516822" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159516822">(Feb 27 2019 at 14:09)</a>:</h4>
<p>could you post a link to that in the issue?</p>



<a name="159519390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159519390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159519390">(Feb 27 2019 at 14:32)</a>:</h4>
<p>there is no link</p>



<a name="159519451"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159519451" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159519451">(Feb 27 2019 at 14:33)</a>:</h4>
<p>the meeting was this week, that was agreed there<br>
there might be an updated paper in the post-meeting mailing , but those are not out yet</p>



<a name="159519523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159519523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159519523">(Feb 27 2019 at 14:34)</a>:</h4>
<p>if non-transient allocations are allowed (in C++23), initializing a <code>constinit</code> from a constant-expression that refers to them would just be ill-formed</p>



<a name="159519549"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159519549" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159519549">(Feb 27 2019 at 14:34)</a>:</h4>
<blockquote>
<p>there is no link</p>
</blockquote>
<p>wait where did you get the minutes then?</p>



<a name="159524937"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159524937" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159524937">(Feb 27 2019 at 15:39)</a>:</h4>
<p><code>constinit</code> is useful so that you never get static initialization order fiasco</p>



<a name="159525042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159525042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159525042">(Feb 27 2019 at 15:40)</a>:</h4>
<p>Rust doesn't have this problem, because it's statics are always initialized at compile time</p>



<a name="159525056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159525056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159525056">(Feb 27 2019 at 15:40)</a>:</h4>
<p>but C++'s statics can be initialized at runtime</p>



<a name="159525443"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159525443" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159525443">(Feb 27 2019 at 15:44)</a>:</h4>
<p><span class="user-mention" data-user-id="125253">@Nicole Mazzuca</span> doesn’t constexpr already guarantee that?</p>



<a name="159525482"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159525482" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159525482">(Feb 27 2019 at 15:45)</a>:</h4>
<p><code>constexpr T myglobal { ... }</code> is what I used.</p>



<a name="159525500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159525500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159525500">(Feb 27 2019 at 15:45)</a>:</h4>
<p>I guess that makes <code>myglobal</code> const.</p>



<a name="159526849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159526849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159526849">(Feb 27 2019 at 16:01)</a>:</h4>
<p>yeah</p>



<a name="159526933"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159526933" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159526933">(Feb 27 2019 at 16:02)</a>:</h4>
<p>you can't have non-const constexpr variables</p>



<a name="159530720"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159530720" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159530720">(Feb 27 2019 at 16:41)</a>:</h4>
<blockquote>
<p>wait where did you get the minutes then?</p>
</blockquote>
<p>Obviously gnzlbg has bugged the standards meeting.</p>



<a name="159535582"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159535582" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159535582">(Feb 27 2019 at 17:42)</a>:</h4>
<p><span class="user-mention" data-user-id="125253">@Nicole Mazzuca</span> Rust does not have C++ static, Rust statics are C++ <code>constinit</code></p>



<a name="159535586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159535586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159535586">(Feb 27 2019 at 17:42)</a>:</h4>
<p>yes</p>



<a name="159535601"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159535601" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159535601">(Feb 27 2019 at 17:42)</a>:</h4>
<p>that is what I said</p>



<a name="159538171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159538171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159538171">(Feb 27 2019 at 18:13)</a>:</h4>
<p>i think it would be fine to just fail when trying to initialize a Rust variable with a const fn that allows allocations to escape</p>



<a name="159538413"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159538413" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159538413">(Feb 27 2019 at 18:14)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> <a href="https://www.reddit.com/r/cpp/comments/au0c4x/201902_kona_iso_c_committee_trip_report_c20/" target="_blank" title="https://www.reddit.com/r/cpp/comments/au0c4x/201902_kona_iso_c_committee_trip_report_c20/">https://www.reddit.com/r/cpp/comments/au0c4x/201902_kona_iso_c_committee_trip_report_c20/</a> makes it seem like <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0784r5.html" target="_blank" title="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0784r5.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0784r5.html</a> was accepted</p>



<a name="159538429"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159538429" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159538429">(Feb 27 2019 at 18:14)</a>:</h4>
<p>it wasn't yet merged but that's editorial</p>



<a name="159538450"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159538450" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159538450">(Feb 27 2019 at 18:15)</a>:</h4>
<p>it was accepted with modifications, in particular, non-transient allocations were removed</p>



<a name="159538508"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159538508" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159538508">(Feb 27 2019 at 18:16)</a>:</h4>
<p>"Allow constexpr allocation, but disallow allocations that are not deleted at compile time." I think that's what they mean by "disallow allocations not deleted at compile-time"</p>



<a name="159538562"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159538562" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159538562">(Feb 27 2019 at 18:16)</a>:</h4>
<p>ah, okay</p>



<a name="159538581"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159538581" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159538581">(Feb 27 2019 at 18:16)</a>:</h4>
<p>the reason was "there is not enough time"</p>



<a name="159538586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159538586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159538586">(Feb 27 2019 at 18:16)</a>:</h4>
<p>well, I still think it's a useful thing to pay attention to, since I'm fairly certain that it will be added in 23, then</p>



<a name="159538608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159538608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159538608">(Feb 27 2019 at 18:17)</a>:</h4>
<p>maybe they try to propose a good solution for the next meeting</p>



<a name="159538638"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159538638" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159538638">(Feb 27 2019 at 18:17)</a>:</h4>
<p>but they might do in a separate paper, to not block that one</p>



<a name="159538675"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159538675" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159538675">(Feb 27 2019 at 18:17)</a>:</h4>
<p>it seems that this meeting was the first time that this paper was really reviewed, and that a lot of people had problems trying to understand what the problem actually was</p>



<a name="159538764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159538764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159538764">(Feb 27 2019 at 18:18)</a>:</h4>
<p>I'm not sure I understand all the problems discussed, but I think this is the crux of it:</p>
<div class="codehilite"><pre><span></span><span class="k">constexpr</span> <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;&gt;</span> <span class="n">v</span> <span class="o">=</span> <span class="n">foo</span><span class="p">();</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="o">*</span><span class="n">v</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="n">get</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="o">=</span> <span class="mi">42</span><span class="p">;</span> <span class="p">}</span>
</pre></div>



<a name="159538829"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159538829" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159538829">(Feb 27 2019 at 18:19)</a>:</h4>
<p>(note: if you replace <code>constexpr</code> with <code>const</code> this compiles and works fine in C++ today)</p>



<a name="159539252"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159539252" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159539252">(Feb 27 2019 at 18:23)</a>:</h4>
<p>mmh</p>



<a name="159539268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159539268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159539268">(Feb 27 2019 at 18:23)</a>:</h4>
<p>isn't that the point of <code>mark_immutable_in_constexpr</code>?</p>



<a name="159539287"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159539287" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159539287">(Feb 27 2019 at 18:23)</a>:</h4>
<p>so that you can mark, say, <code>vector</code>'s buffer as immutable, but not <code>unique_ptr</code>s</p>



<a name="159561085"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159561085" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159561085">(Feb 27 2019 at 22:52)</a>:</h4>
<p>if the <code>unique_ptr</code>s landed there, they must be pointing to immutable memory as well, right?<br>
so one would somehow need to also mark the memory of the <code>unique_ptr</code> as immutable</p>



<a name="159561205"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159561205" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159561205">(Feb 27 2019 at 22:54)</a>:</h4>
<p>also, the example above should error at compile-time, but for that you need to prove that the code modifies immutable memory at compile-time</p>



<a name="159561353"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159561353" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159561353">(Feb 27 2019 at 22:57)</a>:</h4>
<p>that's not easy because <code>*v.get(0) = 42</code> is "ok" because <code>const</code> is shallow: the pointers in the vector are <code>const</code>, but the memory that they refer to does not get the <code>const</code> propagated to it in the type system</p>



<a name="159561438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159561438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159561438">(Feb 27 2019 at 22:58)</a>:</h4>
<p>so AFAICT one would need to insert run-time checks for this, as in <code>*v.get(0) = 42</code> could try to modify read-only memory if it were to get executed</p>



<a name="159626849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159626849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159626849">(Feb 28 2019 at 16:03)</a>:</h4>
<p>no, because you can also put stuff into rwmem</p>



<a name="159630201"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159630201" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159630201">(Feb 28 2019 at 16:43)</a>:</h4>
<p><span class="user-mention" data-user-id="125253">@Nicole Mazzuca</span> but you can't free static rwmem</p>



<a name="159630414"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159630414" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159630414">(Feb 28 2019 at 16:45)</a>:</h4>
<p>so if you have a <code>constexpr vector&lt;unique_ptr&lt;string&gt;&gt; v = ...;</code>, where the <code>string</code> and its contents are in read-write memory, you could write <code>*v.get(0) = to_string('on the heap"); </code>, which would destroy the string in rw memory, and create a new one, whose content is now in the heap</p>



<a name="159630486"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159630486" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159630486">(Feb 28 2019 at 16:46)</a>:</h4>
<p>the problem there is destroying the string, whose pointer points to the rw memory</p>



<a name="159630508"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159630508" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159630508">(Feb 28 2019 at 16:46)</a>:</h4>
<p>if the destructor tries to free the memory, the behavior is undefined</p>



<a name="159630524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159630524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159630524">(Feb 28 2019 at 16:47)</a>:</h4>
<p>so the destructor needs a way to tell, whether the memory should be freed or not</p>



<a name="159630562"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159630562" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159630562">(Feb 28 2019 at 16:48)</a>:</h4>
<p>that's arguably a useful thing to allow, but at the same time every destructor now needs to do this</p>



<a name="159630703"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159630703" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159630703">(Feb 28 2019 at 16:49)</a>:</h4>
<p>also, run-time code needs to be able to perform this check (given a pointer and a length, is that memory region completely or partially in rw static memory?), so now all code pays for this check</p>



<a name="159631260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159631260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159631260">(Feb 28 2019 at 16:56)</a>:</h4>
<p>mmmmh</p>



<a name="159631262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159631262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159631262">(Feb 28 2019 at 16:56)</a>:</h4>
<p>okay</p>



<a name="159631268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/heap-allocation%20in%20const%20fn/near/159631268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/heap-allocation.20in.20const.20fn.html#159631268">(Feb 28 2019 at 16:56)</a>:</h4>
<p>yeah I see</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>